package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.MaterialType;
import com.scau.service.impl.business.MaterialTypeService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class MaterialTypeAction extends BaseAction implements 
		Serializable{
	private final static Log logger = LogFactory
			.getLog(MaterialTypeAction.class);
	private PageController pager = null;
	private MaterialType materialType = null;
	private MaterialTypeService materialTypeService = null;
	private List<MaterialType> typeList = null;

	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = materialTypeService.getRecordCount(new MaterialType());
		String URL =getListURL();
		pager.setURL(URL);		
		pager.setTotalRowsAmount(totalRows);
		List<MaterialType> resourceList = materialTypeService.list(
				new MaterialType(), this.pager.getPageStartRow(), pager
						.getPageSize(), null,null);
		pager.setData(resourceList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		materialType = materialTypeService.get(materialType);
		typeList = materialTypeService.listAll(new MaterialType());
		request.setAttribute("typeList", typeList);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			materialTypeService.save(materialType);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("materialType", materialType);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] type_ids =request.getParameterValues("typeId");
		for (String typeId : type_ids) {
			MaterialType mt = new MaterialType();
			if (null != typeId && !"0".equals(typeId)) {
				materialTypeService.delete(mt,Long.parseLong(typeId));
			}
		}
		return list();
	}
	

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public List<MaterialType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<MaterialType> typeList) {
		this.typeList = typeList;
	}

	

	public MaterialTypeService getMaterialTypeService() {
		return materialTypeService;
	}

	@Resource
	public void setMaterialTypeService(MaterialTypeService materialTypeService) {
		this.materialTypeService = materialTypeService;
	}

	public MaterialType getMaterialType() {
		return materialType;
	}

	public void setMaterialType(MaterialType materialType) {
		this.materialType = materialType;
	}

	
}
